//
//  XJPayPasswordViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/24.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  支付密码

#import "XJPayPasswordViewController.h"
#import "XJPayPasswordModifyController.h"


#import "XJPayPasswordView.h"
//usermodel
#import "XJUserModel.h"

@interface XJPayPasswordViewController ()
@property(nonatomic, strong) XJPayPasswordView *ppView;

@end

@implementation XJPayPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"支付密码";

    [self.view addSubview:self.ppView];
    
    // 如果已经成功登录就直接显示手机号
    if ([XJUserModel xj_phone]) {
        XJLog(@"有手机号 直接显示。");
        //显示格式 158****444
        self.ppView.ppPhoneTextField.text = [self damaWithPhone:[XJUserModel xj_phone]];
        
        // 改bug: 让发送验证码按钮可点击
        [self.ppView enableSendCodeButton];
    }
}

- (XJPayPasswordView *)ppView {
    if (!_ppView) {
        _ppView = [[XJPayPasswordView alloc] initWithFrame:self.view.bounds];
        
        XJWeakSelf(self);
        _ppView.nextButtonBlock = ^{
            
            [weakself bussinessLogicalOfNextButton]; //处理业务逻辑代码
        };
    }
    return _ppView;
}



// 点击下一步的业务逻辑
- (void)bussinessLogicalOfNextButton {

    // 1.手机号
    NSString *phone = [XJUserModel xj_phone];
    if ([NSString isEmptyString:phone]) {
        [JRToast showWithText:@"请输入手机号"];
        return;
    }
    
    // 2.请输入验证码
    NSString *verifyCode = self.ppView.ppCodeTextField.text;
    if ([NSString isEmptyString:verifyCode]) {
        [JRToast showWithText:@"请输入验证码"];
        return;
    }
    
    // 3-1.调接口验证验证码:成功 跳转下一界面    失败
    [self vefifyCodeRequestWithPhone:phone code:verifyCode];
}
#pragma mark - 验证验证码网络请求
- (void)vefifyCodeRequestWithPhone:(NSString *)phone code:(NSString *)code {
 
    [XJHttpTool requestVerifyCodeWithPhone:phone code:code type:@"editpwdpay" successH:^(id responseObjc) {
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            // 跳转界面
            XJPayPasswordModifyController *modifyVC = [[XJPayPasswordModifyController alloc] init];
            [self.navigationController pushViewController:modifyVC animated:YES];
        }else {
            [JRToast showWithText:responseObjc[@"message"]];
        }

    } failureH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}

#pragma mark -  Private method
//打码:手机号十一位
- (NSString *)damaWithPhone:(NSString *)phone
{
    if (phone.length != 11)  return phone;
    
    NSInteger length = phone.length;
    
    //手机号前3位数 后三位 中间五位数打码
    NSMutableString *daMaStrM = [NSMutableString string];
    
    NSRange range1 = NSMakeRange(0, 3);
    NSRange range2 = NSMakeRange(length-3, 3);
    NSString *str1 = [phone substringWithRange:range1];
    NSString *str2 = [phone substringWithRange:range2];
    
    NSString *centerStr = @"*****";
    
    [daMaStrM appendFormat:@"%@%@%@",str1,centerStr,str2];
    
    XJLog(@"打码后的字符串:%@",daMaStrM);
    return daMaStrM;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
